package vn.edu.uit.game_show;

import vn.edu.uit.game_show.DragDrop.DeleteZone;
import vn.edu.uit.game_show.DragDrop.DragController;
import vn.edu.uit.game_show.DragDrop.DragLayer;
import vn.edu.uit.game_show.DragDrop.DragSource;
import vn.edu.uit.game_show.DragDrop.ImageCell;
import vn.edu.uit.game_show.DragDrop.ImageCellAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GameActivity extends Activity implements View.OnTouchListener,
		View.OnClickListener, View.OnLongClickListener {

	GridView gvNhom;
	GridView gvChuKy;
	GridView gvElements;
	
	Chronometer countTimer;

	LinearLayout llListElements;
	

	String[] elements = {"al", "ar", "as", "b", "be", "br", "c", "ca", "cl", "f", "ga", "ge", "i", "in", "k", "kr", "li", "mg", "n", "na", "ne", "o", "p", "rb", "s", "sb", "se", "si", "sn", "sr", "te", "xe"};
	public String[] check = {  "li", "be", "b", "c", "n", "o", "f", "ne",
						"na", "mg", "al", "si", "p", "s", "cl", "ar",
						"k", "ca", "ga", "ge", "as", "se", "br", "kr",
						"rb", "sr", "in", "sn", "sb", "te", "i", "xe"};
	private DragController mDragController;
	private DragLayer mDragLayer;
	private DeleteZone mDeleteZone;
	private boolean mLongClickStartDrag = false;
	
	Button btnPrev, btnNext, btnCheck;
	HorizontalScrollView horiView;
	
	int resourceID = R.drawable.ic_launcher;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game);

		mDragController = new DragController(this);
		mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
		mDragLayer.setDragController(mDragController);
		mDragController.setDragListener(mDragLayer);
		
		countTimer = (Chronometer) findViewById(R.id.countTimer);
		countTimer.start();

		gvNhom = (GridView) findViewById(R.id.gvNhom);
		gvNhom.setAdapter(new NhomChuKyAdapter(this, true));

		gvChuKy = (GridView) findViewById(R.id.gvChuKi);
		gvChuKy.setAdapter(new NhomChuKyAdapter(this, false));

		gvElements = (GridView) findViewById(R.id.gvElements);
		gvElements.setAdapter(new ImageCellAdapter(this));

		mDragLayer.setGridView(gvElements);
		
		horiView = (HorizontalScrollView) findViewById(R.id.horiView);

		llListElements = (LinearLayout) findViewById(R.id.menuBar);
		
		
		
		for (int i = 0; i < elements.length; i++) {
			// llListElements.addView(element(symbols[i]), 55, 55);

			ImageCell img = new ImageCell(this);
			//img.setImageResource(R.drawable.element);
			
			String image = elements[i].toString();
			try {
				resourceID = R.drawable.class.getField(image).getInt(null);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			img.setImageResource(resourceID);			
			img.setContentDescription("l#"+i+"#"+image);
			//img.setBackgroundResource(R.drawable.untitled);
			
			img.mEmpty = false;
			img.mCellNumber = -1;
			img.setOnTouchListener(this);
			img.setOnClickListener(this);
			img.setOnLongClickListener(this);

			llListElements.addView(img, 52, 50);
		}
		
		btnPrev = (Button) findViewById(R.id.btnPrev);
		btnPrev.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				horiView.scrollBy(-100, 0);
			}
		});
		
		btnNext = (Button) findViewById(R.id.btnNext);
		btnNext.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				horiView.scrollBy(+100, 0);
			}
		});
		
		btnCheck = (Button) findViewById(R.id.btnCheck);
		btnCheck.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//Toast.makeText(getBaseContext(), gvElements.getChildAt(0).getContentDescription().toString(), Toast.LENGTH_SHORT).show();
				
//				for (int j = 0; j < check.length; j++) {
//					String tmp = gvElements.getChildAt(j).getContentDescription().toString();
//					if (tmp.equals("grid")) {
//						Toast.makeText(getBaseContext(), "Sắp xếp chưa đúng", Toast.LENGTH_SHORT).show();
//						return;
//					} else {
//						String[] gridItem = tmp.split("#");
//						if (!gridItem[2].toString().equals(check[j].toString())) {
//							Toast.makeText(getBaseContext(), "Sắp xếp chưa đúng", Toast.LENGTH_SHORT).show();
//							return;
//						}
//					}					
//				}
				
				Toast.makeText(getBaseContext(), "Thành công", Toast.LENGTH_SHORT).show();
				
				countTimer.stop();
				String time = countTimer.getText().toString();
				
				SharedPreferences prefs = getSharedPreferences("Result", 0);
				SharedPreferences.Editor editor = prefs.edit();
				editor.putString("Time", time);
				editor.commit();
				
				Intent intent = new Intent(GameActivity.this, SendResult.class);
				Bundle b = new Bundle();
				b.putString("Time", time);
				intent.putExtras(b);
				startActivity(intent);
				finish();
			}
		});
		
		mDeleteZone = (DeleteZone) findViewById (R.id.delete_zone_view);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public boolean startDrag(View v) {
		DragSource dragSource = (DragSource) v;
		mDragController.startDrag(v, dragSource, dragSource,
				DragController.DRAG_ACTION_MOVE);
		
		if (v.getContentDescription().charAt(0) == 'l') {
			String[] tmp = v.getContentDescription().toString().split("#");
			ImageCell img = new ImageCell(this);
			
			try {
				resourceID = R.drawable.class.getField(tmp[2].toString()).getInt(null);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			img.setImageResource(resourceID);
			img.setContentDescription(v.getContentDescription().toString());
			int i = Integer.parseInt(tmp[1].toString());
			img.mEmpty = false;
			img.mCellNumber = -1;
			img.setOnTouchListener(this);
			img.setOnClickListener(this);
			img.setOnLongClickListener(this);
			llListElements.removeViewAt(i);
			llListElements.addView(img, i, new LayoutParams(52, 50));
			//Toast.makeText(getBaseContext(), ""+i, Toast.LENGTH_SHORT).show();
		} 
		
		return true;
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (mLongClickStartDrag)
			return false;

		boolean handleHere = false;
		final int action = event.getAction();

		if (action == MotionEvent.ACTION_DOWN) {
			handleHere = startDrag(v);
		}

		return handleHere;
	}

	public void onClick(View v) {
		if (mLongClickStartDrag) {
			//
		}
	}

	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		return false;
	}

}
